package com.xqq.service;


import com.xqq.minio.MinioClientManager;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.xqq.config.MinioClientConfig.minioBucketName;


@Service
@Slf4j
public class MinioClientService {

    @Resource
    private MinioClientManager minioClientManager;
    private final MinioClient minioClient;

    @Autowired
    public MinioClientService(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    public String uploadImage(String base64) throws Exception {
        try {
            String fileId = minioClientManager.uploadBase64Image(base64, minioBucketName, minioClient);
            if (StringUtils.isNotBlank(fileId)) {
                return fileId;
            } else {
                throw new Exception("图片上传失败");
            }
        } catch (Exception e) {
            throw new Exception("图片上传失败");
        }
    }
}
